import {
	getModelInfo,
	get_relatedcommendlist
} from '@/static/js/model'
import { completeTask } from "@/static/js/points.js";
export default {
	data() {
		return {
			modelInfo: {},
			id: '',
			imgList: [],
			activeIndex: '',
			dlModelUrl: '',
			downloadFlag: false,
			ratio: 1.41,
			xgtjList: [],
			showPointsTips: false, //任务完成提示
			pointsObj: {}, //任务完成对象
		};
	},
	onLoad(e) {
		if (e.ratio) this.ratio = parseFloat(e.ratio)
		this.id = e.id
		this.getModelInfo();
	},
	methods: {
		// 获取模版信息
		getModelInfo() {
			getModelInfo({
				id: this.id,
			}).then(res => {
				if (res.code == 1) {
					this.modelInfo = res.data;
					this.imgList = res.data.images.split('|');
					this.dlModelUrl = res.data.content;
					this.isDownload()
					this.getRelatedcommendlist()
				}
			})
		},

		// 获取相关推荐
		getRelatedcommendlist() {
			var regex = /^https?:\/\/([^\/]+)(\/.*|$)/;
			get_relatedcommendlist({
				title: this.modelInfo.title
			}).then(res => {
				if (res.code == 1) {
					for (let item of res.data.list) {
						if (!item.pic.match(regex)) {
							item.pic = 'https://xzcs-video.oss-cn-beijing.aliyuncs.com' + item.pic
						}
						this.xgtjList.push(item)
					}
				}
			})
		},

		// 相关模版跳转
		xgtjClick(item) {
			uni.showLoading({
				title: '加载中...'
			});
			let aspectRatio = ''
			uni.getImageInfo({
				src: item.pic,
				success: function (res) {
					uni.hideLoading();
					aspectRatio = (res.height / res.width);
					uni.navigateTo({
						url: `/pages/model/modelInfo/index?id=${item.id}&ratio=${aspectRatio}`
					})
				},
				fail: function (error) {
					uni.hideLoading();
					uni.showToast({
						title: '加载失败',
						duration: 2000
					});
				}
			});
		},

		// 预览图片
		previewImg(idx) {
			uni.previewImage({
				current: this.imgList[idx], // 当前显示图片的http链接
				urls: this.imgList // 需要预览的图片http链接列表
			})
		},

		//切换页面
		switchPage(index) {
			this.activeIndex = index
		},
		//当滑块切换
		onSwiperChange(e) {
			this.activeIndex = e.detail.current
		},

		// 看模板
		previewModel() {
			// 检查token
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
				return
			}
			// 获取缓存列表
			const cacheList = uni.getStorageSync("dlModelList") || [];
			// 查找符合条件的项
			const item = cacheList.find(item => item.netUrl === this.dlModelUrl && item.localPath !== null && item
				.localPath !== "");
			// 如果找到，打开文档
			if (item) {
				this.completeQuest("A3");
				setTimeout(() => {
					uni.openDocument({
						filePath: item.localPath
					});
				}, 1500)
			}
		},

		//下载模版
		downloadModel(e) {
			const that = this;
			if (!uni.getStorageSync("token")) {
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return;
			}
			uni.downloadFile({
				url: this.dlModelUrl,
				success: (data) => {
					if (data.statusCode === 200) {
						const cacheList = uni.getStorageSync('dlModelList') || [];
						const itemIndex = cacheList.findIndex(item => item.netUrl === that.dlModelUrl &&
							item.localPath !== null && item.localPath !== "");
						if (itemIndex !== -1) {
							cacheList.splice(itemIndex, 1);
						}
						const saveData = {
							netUrl: that.dlModelUrl,
							localPath: data.tempFilePath,
							modelId: that.id,
							name: that.modelInfo.title
						};
						cacheList.push(saveData);
						uni.setStorageSync('dlModelList', cacheList);
						that.getModelInfo();
						if (e === 1) {
							that.previewModel();
						}
					}
				},
				fail: (err) => {
					console.log(err);
					uni.showToast({
						icon: 'none',
						mask: true,
						title: '失败，请重新下载',
					});
				},
			});
		},

		// 判断是否已下载
		isDownload() {
			let cacheList = uni.getStorageSync("dlModelList") || [];
			let hasDownload = cacheList.some(item => item.netUrl === this.dlModelUrl && item.localPath !== null && item
				.localPath !== "");
			this.downloadFlag = hasDownload;
		},

		//完成任务
		completeQuest(code) {
			completeTask({ code }).then((res) => {
				if (res.code == 1) {
					setTimeout(() => {
						this.showPointsTips = true;
					}, 1000);
					this.pointsObj = res.data;
				}
			});
			setTimeout(() => {
				this.showPointsTips = false;
			}, 3000);
		},
	},
}